import requests

DEEPSEEK_API_URL = "https://api.deepseek.com/v1/chat/completions"
DEEPSEEK_API_KEY = "你的API密钥"

def ask_llm(prompt):
    """
    调用Deepseek大模型API进行问答。
    :param prompt: 用户输入文本
    :return: LLM回复内容
    """
    if not DEEPSEEK_API_KEY or DEEPSEEK_API_KEY == "你的API密钥":
        print("请在llm/deepseek_api.py中填写你的Deepseek API密钥！")
        return "[API密钥未配置]"
    headers = {
        "Authorization": f"Bearer {DEEPSEEK_API_KEY}",
        "Content-Type": "application/json"
    }
    data = {
        "model": "deepseek-chat",  # 按官方文档填写
        "messages": [
            {"role": "user", "content": prompt}
        ]
    }
    try:
        print("向Deepseek LLM发送请求...")
        resp = requests.post(DEEPSEEK_API_URL, headers=headers, json=data, timeout=20)
        if resp.status_code == 200:
            result = resp.json()
            # 解析返回内容
            content = result["choices"][0]["message"]["content"]
            print("LLM回复：", content)
            return content
        else:
            print("LLM请求失败：", resp.text)
            return f"[LLM请求失败] {resp.text}"
    except Exception as e:
        print(f"LLM请求异常: {e}")
        return f"[LLM请求异常] {e}" 